package vn.wallet.app.managedBean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;

import vn.wallet.app.domain.Category;
import vn.wallet.app.domain.Record;
import vn.wallet.app.helper.AppUtils;
import vn.wallet.app.helper.PageUtils;
import vn.wallet.app.service.ICategoryService;
import vn.wallet.app.service.IRecordService;

@ManagedBean(name = "recordBean")
@ViewScoped
public class RecordBean extends DefaultBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final Logger log = Logger.getLogger(this.getClass());

	// inject service
	@ManagedProperty(value = "#{recordService}")
	private IRecordService recordService;

	@ManagedProperty(value = "#{categoryService}")
	private ICategoryService categoryService;

	private final String Dialog_Add_New_Header = AppUtils.getResource("wallet.app.record.dialog.addNew.header");
	private final String Dialog_Update_Header = AppUtils.getResource("wallet.app.record.dialog.update.header");
	private Record customRecord;
	private boolean isAddNew;
	private List<Record> lstRecord;
	private List<Category> lstCategory;
	private SelectItem[] categoryOption;
	private int selectedId;
	private boolean updateMode;

	public RecordBean() {
		customRecord = new Record(getCurrentUser());
	}

	@Override
	@PostConstruct
	public void prepareData() {
		refreshCategory();
		refreshRecord();
	}

	private void refreshRecord() {
		try {
			lstRecord = recordService.getAllByUserId(currentUser.getId());
		} catch (Exception ex) {
			log.error("Unable to get record data", ex);
			PageUtils.logFailed("Unable to get record data");
		}
	}

	private void refreshCategory() {
		try {
			lstCategory = categoryService.getAllByUserId(currentUser.getId());
			refreshCategoryOption();
		} catch (Exception ex) {
			log.error("Unable to get record data", ex);
			PageUtils.logFailed("Unable to get record data");
		}
	}

	private void refreshCategoryOption() {
		int size = lstCategory.size() + 1;
		categoryOption = new SelectItem[size];
		categoryOption[0] = new SelectItem(0, "");
		for (int i = 1; i < size; i++) {
			categoryOption[i] = new SelectItem(lstCategory.get(i).getId(), lstCategory.get(i).getName());
		}
	}

	@Override
	protected void setPageTitle(String str) {
		super.siteConfig.setSiteTitle("Record Bean");
	}

	public void clickedAddNew() {
		System.out.println("Clicked add new");
		customRecord = new Record();
		customRecord.setUser(currentUser);
		RequestContext.getCurrentInstance().execute(
				"changeDialogHeader('" + Dialog_Add_New_Header + "')");
	}

	/**
	 * if modeId = 0: init new category item<br/>
	 * else update selectedNode by form value
	 * 
	 * @param modeID
	 */
	public void prepareItem(long modeID) {
		// System.out.println("prepareItem");
		if (modeID == 1) { // edit mode
			if (updateMode) {
				updateItem();
			} else {
				saveItem();
			}
		}
		refreshRecord();
		setSelectedId(0);
		RequestContext.getCurrentInstance().update("form_update");
		RequestContext.getCurrentInstance().update("form_action");
		RequestContext.getCurrentInstance().update("form_main_top");
	}

	@Override
	public void saveItem() {
		System.out.println("Clicked Save");
		try {
			recordService.save(customRecord);
		} catch (Exception e) {
			log.error(e);
		}
	}

	@Override
	public void updateItem() {
	}

	@Override
	public void deleteItem() {
	}

	public SelectItem[] getCategoryOption() {
		return categoryOption;
	}

	public Record getCustomRecord() {
		return customRecord;
	}

	public void setCustomRecord(Record customRecord) {
		this.customRecord = customRecord;
	}

	public IRecordService getRecordService() {
		return recordService;
	}

	public void setRecordService(IRecordService recordService) {
		this.recordService = recordService;
	}

	public ICategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(ICategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public List<Category> getLstCategory() {
		return lstCategory;
	}

	public void setLstCategory(List<Category> lstCategory) {
		this.lstCategory = lstCategory;
	}

	public boolean isAddNew() {
		return isAddNew;
	}

	public void setAddNew(boolean isAddNew) {
		this.isAddNew = isAddNew;
	}

	public List<Record> getLstRecord() {
		return lstRecord;
	}

	public void setLstRecord(List<Record> lstRecord) {
		this.lstRecord = lstRecord;
	}

	public int getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(int selectedId) {
		this.selectedId = selectedId;
	}

}
